import { assign } from 'min-dash'
import Common from '../../Common'

// 引入相关的样式
import '@/assets/phuket/css/palette.css'

/**
 * 完全自定义bpmnjs的调色板类
 *
 * @param {*} palette
 * @param {*} create
 * @param {*} elementFactory
 * @param {*} globalConnect
 * @param {*} spaceTool
 * @param {*} lassoTool
 * @param {*} handTool
 * @param {*} bpmnFactory
 */
export default function PhuketPaletteProvider(
  palette,
  create,
  elementFactory,
  globalConnect,
  spaceTool,
  lassoTool,
  handTool,
  bpmnFactory
) {
  this.create = create
  this.elementFactory = elementFactory
  this.globalConnect = globalConnect
  this.spaceTool = spaceTool
  this.lassoTool = lassoTool
  this.handTool = handTool
  this.bpmnFactory = bpmnFactory

  // 定义这是一个palette
  palette.registerProvider(this)
}

// 实例化PaletteProvider并注入参数
PhuketPaletteProvider.$inject = [
  'palette',
  'create',
  'elementFactory',
  'globalConnect',
  'spaceTool',
  'lassoTool',
  'handTool',
  'bpmnFactory'
]

/**
 * 获取Palette列表
 */
PhuketPaletteProvider.prototype.getPaletteEntries = function(element) {
  element

  const {
    create,
    elementFactory,
    handTool,
    lassoTool,
    spaceTool,
    globalConnect,
    bpmnFactory
  } = this

  var actions = {}

  /**
   * 创建操作
   *
   * @param {String} stage 阶段，全局唯一
   * @param {String} type 类型
   * @param {String} group 分组
   * @param {String} name 名称
   * @param {String} title 主题
   * @param {Object} [options]
   */
  function createAction(stage, type, group, name, title, options) {
    // 创建开始
    function createStart(event) {
      const bpmnType = 'bpmn:Activity'
      // 实例化businessObject
      const businessObject = bpmnFactory.create(bpmnType)
      // 赋值名称
      businessObject.name = name
      // 将扩展参数传入businessObject.$attrs中
      businessObject.$attrs.stage = stage
      businessObject.$attrs.type = type
      businessObject.$attrs.group = group
      businessObject.$attrs.title = title
      // 实例化shape
      var attrs = assign({ type: bpmnType, businessObject }, options)
      var shape = elementFactory.createShape(attrs)
      create.start(event, shape)
    }

    return {
      group: group, // 组名（根据组名显示前端会分组归类在一起）
      className: 'pkt-icon', // 类名（根据类名显示前端样式）
      title: title, // 标题
      action: {
        dragstart: createStart, // 拖动开始时触发
        click: createStart // 点击时触发
      }
    }
  }

  // actions添加参数
  assign(actions, {
    'hand-tool': {
      group: 'tools',
      className: 'bpmn-icon-hand-tool',
      title: '抓手工具',
      action: {
        click: function(event) {
          handTool.activateHand(event)
        }
      }
    },
    'lasso-tool': {
      group: 'tools',
      className: 'bpmn-icon-lasso-tool',
      title: '拉索工具',
      action: {
        click: function(event) {
          lassoTool.activateSelection(event)
        }
      }
    },
    'space-tool': {
      group: 'tools',
      className: 'bpmn-icon-space-tool',
      title: '平移工具',
      action: {
        click: function(event) {
          spaceTool.activateSelection(event)
        }
      }
    },
    'global-connect-tool': {
      group: 'tools',
      className: 'bpmn-icon-connection',
      title: '连线工具',
      action: {
        click: function(event) {
          globalConnect.toggle(event)
        }
      }
    },
    'tool-separator': {
      group: 'tools',
      separator: true
    }
  })

  // actions循环添加参数
  const palettes = Common.getPalettes()
  for (const stage in palettes) {
    const item = palettes[stage]
    actions[stage] = createAction(
      stage,
      item.type,
      item.group,
      item.name,
      item.title
    )
  }

  return actions
}
